package top.mcwebsite.markdown.render

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.IntrinsicSize
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import org.intellij.markdown.MarkdownTokenTypes
import top.mcwebsite.markdown.model.MarkdownNode
import top.mcwebsite.markdown.theme.LocalMarkdownTheme

object BlockQuoteComponent : MarkdownComponent {

    @Composable
    override fun render(markdownNode: MarkdownNode) {
        val theme = LocalMarkdownTheme.current
        Row(
            modifier = Modifier
                .padding(theme.blockPadding)
                .background(color = theme.blockQuoteTheme.backgroundColor)
                .fillMaxWidth()
                .height(IntrinsicSize.Min)
        ) {
            Box(
                modifier = Modifier
                    .background(color = theme.blockQuoteTheme.quoteColor)
                    .width(theme.blockQuoteTheme.quoteWidth)
                    .fillMaxHeight()
            )
            Spacer(modifier = Modifier.width(theme.blockQuoteTheme.quoteSpace))
            Column {
                markdownNode.node.children.filterNot { it.type == MarkdownTokenTypes.BLOCK_QUOTE }.forEach {
                    markdownNode.renders[it.type]?.render(
                        MarkdownNode(
                            it,
                            markdownNode.content,
                            markdownNode.state,
                            markdownNode.renders
                        )
                    ) ?: throw IllegalArgumentException("Unknown type: ${it.type}")
                }
            }
        }
    }
}